package com.fair.concurrency.test1;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;

import java.util.Set;
import java.util.stream.Collectors;

public class Test22 {

    public static void main(String[] args) throws Exception {
        Set<Class<?>> instances = getInstances(ParentInterface.class);
        for (Class<?> instance : instances) {
            Object o1 = ReflectUtil.newInstance(instance);
            Object result = ReflectUtil.invoke(o1, "log");
            System.out.println("result = " + result);
        }
    }

    private static Set<Class<?>> getInstances(Class supClass) {
        String aPackage = ClassUtil.getPackage(supClass);
        Set<Class<?>> classes = ClassUtil.scanPackage(aPackage);
        return classes.stream().filter(sonClass -> {
            boolean allAssignableFrom = ClassUtil.isAllAssignableFrom(new Class[]{supClass},
                    new Class[]{sonClass});
            //要将 本身排除
            return allAssignableFrom && sonClass != supClass;
        }).collect(Collectors.toSet());
    }


}
